package arch.chameleon.tools.decode;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Base64批量解码
 * @author George
 *
 */
public class Base64DecoderBatch {
	
	/**
	 * 由多个编码文本批量解码并拼合到一个二进制文件
	 * @param directory 待扫描目录(时间戳或序号文件)
	 * @param targetFile 目标二进制文件
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void decodeToOneBinFile(File directory, File targetFile) throws FileNotFoundException, IOException{
		Validate.notNull(directory);
		Validate.notNull(targetFile);
		if(directory.exists()){
			StringBuilder objBuilder = new StringBuilder();
			File[] aryObjEncodedFiles = directory.listFiles();
			if(aryObjEncodedFiles != null){				
				//读取拼接
				for(File encodeFile : aryObjEncodedFiles){
					BufferedReader objReader = new BufferedReader(new FileReader(encodeFile));
					for(String str = objReader.readLine(); str != null; str = objReader.readLine()){
						objBuilder.append(str);
					}
					IOUtils.closeQuietly(objReader);
				}
				LOG.debug("Base64 Code: {}", new Object[]{objBuilder.toString()});
				//解码
				byte[] aryPrimBin = Base64.decodeBase64(objBuilder.toString());
				BufferedOutputStream objOutput = new BufferedOutputStream(new FileOutputStream(targetFile));
				objOutput.write(aryPrimBin);
				IOUtils.closeQuietly(objOutput);
				LOG.debug("Write finished.");
			}		
		}
	}
	
	
	/**
	 * 由SQLlite数据库批量解码到一个二进制文件
	 * @param SQLliteDbLocation
	 * @param sql
	 * @param targetFile
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public static void decodeToOneBinFile(String SQLiteDbLocation, String sql, File targetFile) throws ClassNotFoundException, SQLException, IOException{
		Class.forName("org.sqlite.JDBC");
		Connection objConn = DriverManager.getConnection("jdbc:sqlite:/" + SQLiteDbLocation);//filename为你的SQLite数据名称
		PreparedStatement objPstmt = objConn.prepareStatement(sql);
		ResultSet objRs = objPstmt.executeQuery();
		
		StringBuilder objBuilder = new StringBuilder();
		while(objRs.next()){
			//加载数据集
			objBuilder.append(objRs.getString(1));
		}
		
		LOG.debug("Base64 Code: {}", new Object[]{objBuilder.toString()});
		
		if(objRs == null)objRs.close();
		if(objPstmt == null)objPstmt.close();
		if(objConn == null)objConn.close();
		
		//解码
		byte[] aryPrimBin = Base64.decodeBase64(objBuilder.toString());
		BufferedOutputStream objOutput = new BufferedOutputStream(new FileOutputStream(targetFile));
		objOutput.write(aryPrimBin);
		IOUtils.closeQuietly(objOutput);
		LOG.debug("Write finished.");	
	}

	
	private transient static final Logger LOG = LoggerFactory.getLogger(Base64DecoderBatch.class);
}
